package ar.com.jwa.domain.emailing;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import ar.com.jwa.domain.User;


public abstract class Mail {
	
	protected String host;
	
	protected String from;
	
	protected User user;
	
	protected String to;
	
	protected Properties props;
	
	protected Session session;
	
	protected MimeMessage message;
	
	protected String subject;
	
	protected BodyPart messageBodyPart;
	
	protected String protocol;
	
	protected String smtpUser;
	
	protected String smtpPassword;
	
	protected MimeMultipart multipart;
	
	
	abstract void prepareMail() throws MessagingException, IOException;
	
	public void send() throws AddressException, MessagingException, IOException {
		
		prepareMail();
		
		Address address = new InternetAddress(this.getFrom());
		Address[] add = {address};
		
		this.props.put("mail.smtp.mail.sender",this.getFrom()); 
		this.props.put("mail.smtps.mail.sender",this.getFrom());
		this.props.put("mail.smtp.host",this.getHost());
		this.props.put("mail.smtp.user",this.getSmtpUser());
		this.props.put("mail.smtp.pass",this.getSmtpPassword());
		this.props.put("mail.smtp.starttls.enable","true");
		this.props.put("mail.smtps.auth", "true");  
		this.props.put("mail.smtps.quitwait", "false");
		this.props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		this.props.put("mail.smtp.socketFactory.fallback", "false");
		this.props.put("mail.transport.protocol",this.getProtocol());
		
		this.message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.getUser().getEmail()));
		this.message.setSubject(this.getSubject());
		this.message.setReplyTo(add);
		this.message.setSender(address);
		this.message.setFrom(address);
		this.message.addFrom(add);
		
		Transport t = session.getTransport(this.protocol);
		t.connect(this.host,(String) props.get("mail.smtp.user"),(String)props.get("mail.smtp.pass"));
		
		this.message.saveChanges();
		
		t.sendMessage(this.message, this.message.getAllRecipients());

		t.close();
	}
		
	
	/**
	 * @return the message
	 */
	public MimeMessage getMessage() {
		return message;
	}
	/**
	 * @param message the message to set
	 */
	public void setMessage(MimeMessage message) {
		this.message = message;
	}
	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}
	/**
	 * @param host the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}
	/**
	 * @return the from
	 */
	public String getFrom() {
		return from;
	}
	/**
	 * @param from the from to set
	 */
	public void setFrom(String from) {
		this.from = from;
	}
	/**
	 * @return the to
	 */
	public User getUser() {
		return user;
	}
	/**
	 * @param to the to to set
	 */
	public void setTo(User to) {
		this.user = to;
	}
	
	public void setTo(String to) {
		this.to = to;
	}
	
	/**
	 * @return the props
	 */
	public Properties getProps() {
		return props;
	}
	/**
	 * @param props the props to set
	 */
	public void setProps(Properties props) {
		this.props = props;
	}
	/**
	 * @return the session
	 */
	public Session getSession() {
		return session;
	}
	/**
	 * @param session the session to set
	 */
	public void setSession(Session session) {
		this.session = session;
	}

	/**
	 * @return the messageBodyPart
	 */
	public BodyPart getMessageBodyPart() {
		return messageBodyPart;
	}

	/**
	 * @param messageBodyPart the messageBodyPart to set
	 */
	public void setMessageBodyPart(BodyPart messageBodyPart) {
		this.messageBodyPart = messageBodyPart;
	}

	/**
	 * @return the subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * @param subject the subject to set
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @return the protocol
	 */
	public String getProtocol() {
		return protocol;
	}

	/**
	 * @param protocol the protocol to set
	 */
	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	/**
	 * @return the smtpUser
	 */
	public String getSmtpUser() {
		return smtpUser;
	}

	/**
	 * @param smtpUser the smtpUser to set
	 */
	public void setSmtpUser(String smtpUser) {
		this.smtpUser = smtpUser;
	}

	/**
	 * @return the smtpPassword
	 */
	public String getSmtpPassword() {
		return smtpPassword;
	}

	/**
	 * @param smtpPassword the smtpPassword to set
	 */
	public void setSmtpPassword(String smtpPassword) {
		this.smtpPassword = smtpPassword;
	}

	/**
	 * @return the multipart
	 */
	public MimeMultipart getMultipart() {
		return multipart;
	}

	/**
	 * @param multipart the multipart to set
	 */
	public void setMultipart(MimeMultipart multipart) {
		this.multipart = multipart;
	}
	
	
}
